
!function(window){
    "use strict";
    window.app = {};
    (function(){
        var path = com.getEnv();
        var base = '';
        this.ajax = function(option){
            //abort
            return $.ajax({
                type:option.type,
                url:option.url,
                data:option.data || {},
                async:option.async === false ? false : true,
                success:function(data,status){
                    option.success && option.success(data,status)
                },
                error:function(xhr,err,msg){
                    if(option.error){
                        option.error(xhr,err,msg);
                    }else if(err == 'timeout'){
                        $.comMessage({
                            type:'warning',
                            text:'网络请求超时！',
                            time:5000
                        })
                        console.log('web-网络请求超时');
                    }else{
                        $.comMessage({
                            type:'warning',
                            text:'网络请求错误！'
                        })
                        console.log('web-网络请求错误');
                    }
                },
                timeout:option.timeout || 8000,
                //headers:{'apiToken':para.token2},
                beforeSend:function(xhr){
                    option.beforeSend && option.beforeSend(xhr)
                },
                complete: function (XMLHttpRequest, status) {
                    option.complete && option.complete(XMLHttpRequest,status);
                }
            });
        };

        this.api = function(url){
            if(path == 'dev'){
                base = "http://customer.beta.orderplus.com";
            }else if(path == 'testing'){
                base = "http://customer.beta.orderplus.com";
            }else if(path == 'production'){
                base = "http://www.allindesk.com"
            }
            return base + url;// + "&date=" + new Date().getTime();
        };

        this.img = function(url){
            if(path == 'dev'){
                base = "";
            }else if(path == 'test'){
                base = "";
            }else{
                base = ""
            }
            return base + url;
        };

        this.get = function(url,callback,errCallback){
            if(path == 'dev'){
                base = "/views/";
            }else if(path == 'testing'){
                base = "/views/";
            }else{
                base = "/views/"
            }
            base  = base + url + '/tmp.html?' + "date=" + new Date().getTime();
            $.get(base,function(data,status){
                callback(data,status)
            }).error(function(status){
                if(errCallback){
                    errCallback(status)
                }else{
                    console.log(status)
                }
            });
        };

        this.dynamicLoadJs = function(arr,position,callback) {
            var isTrue = {};
            var end = true;
            function load(who,url){
                var head = document.getElementById(position);
                var script = document.createElement('script');
                script.type = 'text/javascript';
                script.src = url;
                if(typeof(callback)=='function'){
                    script.onload = script.onreadystatechange = function () {
                        if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete"){
                            script.onload = script.onreadystatechange = null;
                            isTrue[who] = true;
                            end = true;
                            $.each(isTrue,function(index,item){
                                if(!item){
                                    end = false;
                                    return false;
                                }
                            })
                            if(end){callback();}
                        }
                    };
                }
                head.appendChild(script);
            }
            arr.forEach(function(item,index){
                isTrue[index] = false;
            })
            $.each(arr,function(index,item){
                load(index,item)
            })
        }

    }).apply(app);

}(window);




